package com.xiaoying.server.publish.comomon.upyun;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

public class UpYunUtil {

    // 运行前先设置好以下三个参数
    public static final String BUCKET_NAME_VIDEO = "xiaoyingcovideo1";
    public static final String BUCKET_NAME_IMAGE = "xiaoyingcoimage1";
    public static final String USER_NAME = "jiandong";
    public static final String USER_PWD = "jiandong";
    public static final short FILETYPE_VIDEO = 3;
    public static final short FILETYPE_IMAGE_COVER = 4;
    public static final short FILETYPE_IMAGE_POSTER = 5;
    
    /**
     * Video最大重传次数
     */
    public static final int MAX_VIDEO_UPLOAD_COUNT = 3;
    
    /**
     * Image最大重传次数
     */
    public static final int MAX_IMAGE_UPLOAD_COUNT = 3;
    
    
    /** 根目录 */
    public static final String DIR_ROOT = "/";
    
    /** 测试目录 **/
    public static final String DIR_TEST = "/jiandong";
    private static UpYun upYunVideo;
    private static UpYun upYunImage;

    public UpYunUtil(){

    }
    
    /**
     * 获取UpYun的实例
     * 2个实例:UpYunVideo,UpYunImage
     * @author ZJD
     * @param fileType
     * @return
     */
    private static UpYun getUpYunInstance(short fileType){    	
    	if(fileType==FILETYPE_VIDEO){
    		if(upYunVideo==null){
    			upYunVideo = new UpYun(UpYunUtil.BUCKET_NAME_VIDEO, UpYunUtil.USER_NAME, UpYunUtil.USER_PWD);
    			upYunVideo.setDebug(true);
    		}
    		return upYunVideo;
    	}else {
    		if(upYunImage==null){
    			upYunImage = new UpYun(UpYunUtil.BUCKET_NAME_IMAGE, UpYunUtil.USER_NAME, UpYunUtil.USER_PWD);
    			upYunImage.setDebug(true);
    		}
    		return upYunImage;
    	}
    }
	
	
    /**
     * 上传文件
     * @author ZJD
     * @throws IOException
     */
    public static boolean upLoadFile(String filePath,File file,int fileType) throws IOException {
    	if(fileType==FILETYPE_VIDEO){
    		return upYunVideo.writeFile(filePath, file, true);  
    	}else{
    		return upYunImage.writeFile(filePath, file, true);
    	}
    }
    
	
	/**
	 * 创建目录
	 * @author ZJD
	 */
    public static boolean MkDir(String dir,short fileType) {
    	if(fileType==FILETYPE_VIDEO){
    		return getUpYunInstance(fileType).mkDir(dir, true);  
    	}else{
    		return getUpYunInstance(fileType).mkDir(dir, true);
    	}
    }
    
    private static String isSuccess(boolean result) {
        return result ? " 成功" : " 失败";
    }
    

	
	
}
